package com.shrct.businessprocess.product.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.product.RCCompanyService;
import com.shrct.businessprocess.product.RCCompanyServiceEntry;
import com.shrct.businessprocess.product.RCDerivedFullServiceProductSpecification;
import com.shrct.businessprocess.product.RCFullServiceProductEntry;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntry;
import com.shrct.businessprocess.product.RCLogicProduct;
import com.shrct.businessprocess.product.RCProductServiceTracer;
import com.shrct.businessprocess.product.RCProductServiceTracerDAO;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.product.RCProductSpecificationDAO;
import com.shrct.businessprocess.product.RCServiceLevel;
import com.shrct.businessprocess.product.RCServiceResponseTime;
import com.shrct.businessprocess.product.impl.RCProductServiceTracerImpl;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.IHasServicePurchase;
import com.shrct.businessprocess.purchase.RCBudgetContract;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.mylang.pf.BaseDAOService;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Collection;
import java.util.Date;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.product.RCProductServiceTracerDAO")
public class RCProductServiceTracerDAOImpl extends BaseDAOService<RCProductServiceTracer> implements RCProductServiceTracerDAO {
  public Collection<RCProductServiceTracer> findByBudgetContract(final RCBudgetContract budgetContract) {
    IGenericQuery<RCProductServiceTracer> query=getObjectFactory().createGenericQuery(RCProductServiceTracer.class,"select e from com.shrct.businessprocess.product.impl.RCProductServiceTracerImpl as e   where ((e.budgetContract) = (:budgetContract)) and  e.active = 1      ").setParameter("budgetContract",budgetContract);
    Collection<RCProductServiceTracer> _list = query.list();
    return _list;
  }
  
  public RCProductServiceTracer findByProductAndServiceName(final RCIdentifiablePhysicalProductEntry product, final String serviceName) {
    RCProductServiceTracer _xblockexpression = null;
    {
      IGenericQuery<RCProductServiceTracer> query=getObjectFactory().createGenericQuery(RCProductServiceTracer.class,"select e from com.shrct.businessprocess.product.impl.RCProductServiceTracerImpl as e   where ((e.product) = (:product)) and  e.active = 1      ").setParameter("product",product);
      final Collection<RCProductServiceTracer> candidates = query.list();
      RCProductServiceTracer result = null;
      boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(serviceName);
      if (_isNullOrEmpty) {
        final Function1<RCProductServiceTracer,Boolean> _function = new Function1<RCProductServiceTracer,Boolean>() {
            public Boolean apply(final RCProductServiceTracer it) {
              RCCompanyServiceEntry _companyService = it.getCompanyService();
              RCCompanyService _companyService_1 = _companyService.getCompanyService();
              String _name = _companyService_1.getName();
              boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(_name);
              return Boolean.valueOf(_isNullOrEmpty);
            }
          };
        RCProductServiceTracer _findFirst = IterableExtensions.<RCProductServiceTracer>findFirst(candidates, _function);
        result = _findFirst;
        boolean _equals = Objects.equal(result, null);
        if (_equals) {
          IterableExtensions.<RCProductServiceTracer>head(candidates);
        }
      } else {
        final Function1<RCProductServiceTracer,Boolean> _function_1 = new Function1<RCProductServiceTracer,Boolean>() {
            public Boolean apply(final RCProductServiceTracer it) {
              RCCompanyServiceEntry _companyService = it.getCompanyService();
              RCCompanyService _companyService_1 = _companyService.getCompanyService();
              String _name = _companyService_1.getName();
              boolean _equals = serviceName.equals(_name);
              return Boolean.valueOf(_equals);
            }
          };
        RCProductServiceTracer _findFirst_1 = IterableExtensions.<RCProductServiceTracer>findFirst(candidates, _function_1);
        result = _findFirst_1;
      }
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public RCProductServiceTracer create(final RCBudgetContract budgetContract, final Procedure1<? super RCProductServiceTracer> tracerInit) {RCProductServiceTracer rCProductServiceTracer=RCProductServiceTracerImpl.create(getObjectFactory());
    final Procedure1<RCProductServiceTracer> _function = new Procedure1<RCProductServiceTracer>() {
        public void apply(final RCProductServiceTracer it) {
          it.setBudgetContract(budgetContract);
          tracerInit.apply(it);
        }
      };
    RCProductServiceTracer _doubleArrow = ObjectExtensions.<RCProductServiceTracer>operator_doubleArrow(rCProductServiceTracer, _function);
    return _doubleArrow;
  }
  
  public void delete(final RCProductServiceTracer tracer) {getObjectFactory().delete(tracer);
    
  }
  
  @Autowired
  private RCProductSpecificationDAO dao;
  
  public RCFullServiceProductEntry acceptProductService(final IHasServicePurchase hasServicePurchase, final String name, final String serviceSpec, final String serviceContractCode, final RCSupplier productSupplier, final RCIdentifiablePhysicalProductEntry product, final RCServiceLevel serviceLevel, final RCServiceResponseTime serviceResponseTime, final Date startDate, final Date dueDate, final RCProjectMoney price, final Boolean forPartDevice) {
    RCFullServiceProductEntry _xblockexpression = null;
    {
      Iterable<RCProductServiceTracer> _rCProductServiceTracer = hasServicePurchase.getRCProductServiceTracer();
      final Function1<RCProductServiceTracer,RCIdentifiablePhysicalProductEntry> _function = new Function1<RCProductServiceTracer,RCIdentifiablePhysicalProductEntry>() {
          public RCIdentifiablePhysicalProductEntry apply(final RCProductServiceTracer it) {
            return product;
          }
        };
      Iterable<RCIdentifiablePhysicalProductEntry> _map = IterableExtensions.<RCProductServiceTracer, RCIdentifiablePhysicalProductEntry>map(_rCProductServiceTracer, _function);
      final Function1<RCIdentifiablePhysicalProductEntry,Boolean> _function_1 = new Function1<RCIdentifiablePhysicalProductEntry,Boolean>() {
          public Boolean apply(final RCIdentifiablePhysicalProductEntry it) {
            boolean _or = false;
            boolean _equals = Objects.equal(it, product);
            if (_equals) {
              _or = true;
            } else {
              RCIdentifiablePhysicalProductEntry _parent = product.getParent();
              boolean _equals_1 = Objects.equal(it, _parent);
              _or = (_equals || _equals_1);
            }
            return Boolean.valueOf(_or);
          }
        };
      final boolean productValid = IterableExtensions.<RCIdentifiablePhysicalProductEntry>exists(_map, _function_1);
      Validates.Assert(productValid, "\u8BBE\u5907\u4E0D\u5728\u9879\u76EE\u5F85\u8D2D\u4E70\u670D\u52A1\u7684\u6E05\u5355\u4E2D");
      Double _betweenMonth = DateUtil.betweenMonth(startDate, dueDate);
      double _plus = ((_betweenMonth).doubleValue() + 0.5);
      final int months = Double.valueOf(_plus).intValue();
      RCProductSpecification _type = product.getType();
      final RCDerivedFullServiceProductSpecification s = this.dao.getFullService(_type, serviceSpec, serviceLevel, serviceResponseTime, Integer.valueOf(months));
      RCHasPurchaseBudget _asBudget = hasServicePurchase.asBudget();
      RCCostBreakDown _cbd = _asBudget.getCbd();
      RCProject _project = _cbd.getProject();
      final String projectid = _project.getCode();
      RCLogicProduct _logicProduct = product.getLogicProduct();
      RCFullServiceProductEntry _registerFullServiceProduct = _logicProduct.registerFullServiceProduct(name, projectid, serviceContractCode, s, startDate, dueDate);
      final Procedure1<RCFullServiceProductEntry> _function_2 = new Procedure1<RCFullServiceProductEntry>() {
          public void apply(final RCFullServiceProductEntry it) {
            it.setSupplier(productSupplier);
          }
        };
      final RCFullServiceProductEntry entry = ObjectExtensions.<RCFullServiceProductEntry>operator_doubleArrow(_registerFullServiceProduct, _function_2);
      final RCProductServiceTracer tracer = this.findByProductAndServiceName(product, name);
      boolean _or = false;
      boolean _and = false;
      boolean _equals = Objects.equal(tracer, null);
      if (!_equals) {
        _and = false;
      } else {
        _and = (_equals && (forPartDevice).booleanValue());
      }
      if (_and) {
        _or = true;
      } else {
        boolean _and_1 = false;
        boolean _notEquals = (!Objects.equal(tracer, null));
        if (!_notEquals) {
          _and_1 = false;
        } else {
          boolean _not = (!forPartDevice);
          _and_1 = (_notEquals && _not);
        }
        _or = (_and || _and_1);
      }
      Validates.Assert(_or, "\u6CA1\u6709\u627E\u5230\u8BE5\u8BBE\u5907\u7684\u627F\u8BFA\u670D\u52A1");
      boolean _not_1 = (!forPartDevice);
      if (_not_1) {
        tracer.serviceImported(entry);
      }
      _xblockexpression = (entry);
    }
    return _xblockexpression;
  }
}
